import {ref, reactive} from 'vue';
import {myOverListDetails, runErrandOverListDetails} from '@/utlis/interface/Wode'
import {onLoad} from "@dcloudio/uni-app";
import {getToPhone} from "@/utlis/uni_api";

export default (Proyx: any, Type: any) => {
    const deltails = reactive<any>({
        itemDelaits: {},
        goodsLength: null,
        goodsNumall: null
    })
    const phoneListShow = ref(false)
    // 电话联系弹出层
    const phoneBtnList = ref({
        arr: [],
        type: '',
        orderId: '',
        riderPhone: ""
    })
    let orderId: any = ref('')

    onLoad((options) => {
        // #ifdef APP
        // 获取手机通话权限 放到onshow中会重复触发onshow不知道因为什么  放到这个里面吧
        getToPhone().then(res => {
            uni.setStorageSync('getToPhone', res)
        })
        // #endif
    })

    const thisOrderDeltails = (id: number | string) => {
        orderId.value = id
        myOverListDetails(id, Proyx).then((res: any) => {
            if (res.code == 0) {
                deltails.itemDelaits = res.data
                deltails.goodsLength = res.data.goodsVOS.length
                res.data.goodsVOS?.forEach((item: any, index: number | string) => {
                    deltails['goodsNumall'] += item.goodsNum
                });
            }
        }).catch((err: any) => {
            Proyx.$API(Proyx).Toast(err.msg, 'error')
        })
    }

    const runErrandsDeltails = (id: number | string) => {
        orderId.value = id
        runErrandOverListDetails(id, '', Proyx).then((res: any) => {
            if (res.code == 0) {
                deltails.itemDelaits = res.data
                deltails.goodsLength = res.data.goodsVOS.length
                res.data.goodsVOS?.forEach((item: any, index: number | string) => {
                    deltails['goodsNumall'] += item.goodsNum
                });
            }

        }).catch((err: any) => {
            // Proyx.$API(Proyx).Toast(err.msg, 'error')

        })
    }

    function onPhoneClick() {
        console.log("用户 phone = ", deltails.itemDelaits.phone)
        phoneListShow.value = true

        //外卖
        if (Type == 0) {
            //B C 分别表示商家和用户
            phoneBtnList.value = {
                type: deltails.itemDelaits.riderClientStatus,
                riderPhone: deltails.itemDelaits.riderPhone,
                orderId: orderId.value,
                arr: [
                    {
                        title: '联系商家',
                        phone: deltails.itemDelaits.pickPhone,
                        type: "B"
                    },
                    {
                        title: '联系用户',
                        phone: deltails.itemDelaits.phone,
                        type: "C"
                    },
                    {
                        title:'注册用户',
                        phone:deltails.itemDelaits.phone,
                        type: "C0"
                    },
                ] as any
            }
        }
        // 跑腿
        else {
            //C1 C2 分别表示取货人和收货人。如果是就近购买，没有取货人只有收货人，则收货人变成C1
            phoneBtnList.value = {
                type: deltails.itemDelaits.riderClientStatus,
                riderPhone: deltails.itemDelaits.riderPhone,
                orderId: orderId.value,

                // sfBuy 是否是就近购买？ 是：只有收货人，不是：有取货人和收货人
                arr: deltails.itemDelaits.sfBuy ? [
                    {
                        title: '收货人',
                        phone: deltails.itemDelaits.phone,
                        type: 'C1'
                    }
                ] as any : [
                    {
                        title: '取货人',
                        phone: deltails.itemDelaits.pickPhone,
                        type: "C1"
                    },
                    {
                        title: '收货人',
                        phone: deltails.itemDelaits.phone,
                        type: 'C2'
                    }
                ] as any
            }
        }
    }

    /**
     * 取消拨打电话
     */
    const btnCancel = () => {
        phoneListShow.value = false
    }

    return {thisOrderDeltails, deltails, runErrandsDeltails, onPhoneClick, btnCancel, phoneBtnList, phoneListShow}
}
